package com.yxkj.syh.app.huarong.activities.orders.pay_detail;

import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;

import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.syh.app.basic.base.BaseActivity;
import com.yxkj.syh.app.huarong.BR;
import com.yxkj.syh.app.huarong.R;
import com.yxkj.syh.app.huarong.bean.PayOrderInfo;
import com.yxkj.syh.app.huarong.constants.ArouterPath;
import com.yxkj.syh.app.huarong.databinding.ActivityPayOrderDetailBinding;

/**
 * 支出单详情
 */
@Route(path = ArouterPath.PAY_ORDER_DETAIL_ACTIVITY)
public class PayOrderDetailActivity extends BaseActivity<ActivityPayOrderDetailBinding, PayOrderDetailVM> {

    @Autowired
    long id;//支出单id

    @Override
    protected int getContentLayout() {
        return R.layout.activity_pay_order_detail;
    }

    @Override
    protected int getViewModelId() {
        return BR.payOrderDetailVM;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {

        mVDBinding.rv1.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        mVDBinding.rv2.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        mVDBinding.rv1.setAdapter(mViewModel.getPayOrderDetail1Adp1());
        mVDBinding.rv2.setAdapter(mViewModel.getPayOrderDetail1Adp2());

        mVDBinding.btnReceiveMoney.setOnClickListener(v -> {
            if (mViewModel.mldDetailInfo.getValue()==null) return;
            mViewModel.payOrderReceiveMoney(mViewModel.mldDetailInfo.getValue().getId()+"");
        });

        mVDBinding.llContent.showLoading();

        mVDBinding.titleView.setLeftOnClickListener(v -> finish());

    }

    @Override
    protected void initData() {
        mViewModel.payOrderDetail(id);
    }

    @Override
    protected void initObservers() {
        super.initObservers();
        mViewModel.mldDetailInfo.observe(this, payOrderInfo -> {
            if (payOrderInfo == null) {
                if (mVDBinding.llContent.isLoadingCurrentState()){
                    mVDBinding.llContent.showError(0, "", "", "重试", v -> {
                        mViewModel.payOrderDetail(id);
                        mVDBinding.llContent.showLoading();
                    });
                }
            } else {
                mViewModel.setData(payOrderInfo);
                if (mVDBinding.llContent.isLoadingCurrentState()){
                    mVDBinding.llContent.showContent();
                }
            }
        });

        mViewModel.mldReceiveMoney.observe(this, aBoolean -> {
            if (aBoolean){
                mVDBinding.llContent.showLoading();
                mViewModel.payOrderDetail(id);
            }
        });
    }
}
